package com.margo.project.curator.action;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class MargoZookeeperController {
	@Autowired
	private CuratorFramework curatorFramework;
	
	@Autowired
	private NodeCache nodeCache;
	
	@RequestMapping("/create")
	@ResponseBody
	public String createNode() throws Exception {
		curatorFramework.create().forPath("/margo", "data".getBytes());
		return "sucess";
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String deleteNode() throws Exception {
		curatorFramework.delete().withVersion(0).forPath("/margo");
		return "sucess";
	}
	
	
	@RequestMapping("/watches")
	@ResponseBody
	public String watchsDeleteNode() throws Exception {
		nodeCache.getListenable().addListener(new NodeCacheListener() {
			public void nodeChanged() throws Exception {
				// TODO Auto-generated method stub
				System.out.println(nodeCache.getCurrentData());
			}
		});
		return "sucess";
	}

}
